package com.citywithincity.db.sync;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.SchedulerException;
import org.w3c.dom.Element;

import com.citywithincity.utils.PathUtil;
import com.damai.jobcenter.QuartzPlugin;
import com.damai.xml.XMLParseException;
import com.damai.xml.XMLUtil;
import com.damai.xml.XMLVisitor;

/**
 * 同步数据的
 */
public class DbSyncUtil {
	/**
	 * 全部数据同步
	 */
	public static final String METHOD_ALL = "all";

	/**
	 * 分页同步
	 */
	public static final String METHOD_PAGE = "page";

	
	
	
	
	public static void fromXml(String name){
		try {
			XMLUtil.parseXMLLevel1(PathUtil.getConfigInputStream(DbSyncUtil.class, name), itemVisiter);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (XMLParseException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	
	
	
	private static final String enabled = "enabled";
	
	/**
	 * 
	 */
	private static final XMLVisitor itemVisiter = new XMLVisitor() {
		@Override
		public void onVisit(Element element) {
			boolean e = true;
			if(element.hasAttribute(enabled) && element.getAttribute(enabled).equals("false")){
				e = false;
			}
			
			if(!e)return;
			//数据
			Map<String, Object> data = new HashMap<String, Object>();
			int targetDb = Integer.parseInt(element.getAttribute("targetDb"));
			int srcDb = Integer.parseInt(element.getAttribute("srcDb"));
			String method = element.getAttribute("method");
			
			String insert = element.getAttribute("insert");
			String srcTable = element.getAttribute("srcTable");
			String targetTable = element.getAttribute("targetTable");
			String dataHandler = element.getAttribute("dataHandler");
			String uniqueKeys = element.getAttribute("uniqueKeys");
			String condition = element.getAttribute("condition");
			String name = element.getAttribute("name");
			//完成之后
			String complete = element.getAttribute("complete");		//SyncComplete
			
			String extraFields = element.getAttribute("extraFields");
			
			String after = element.getAttribute("after");
			
			
			data.put("after", after);
			data.put("complete", complete);
			data.put("condition", condition);
			data.put("name", name);
			data.put("targetDb", targetDb);
			data.put("srcDb", srcDb);
			data.put("method", method);
			
			data.put("insert", insert);
			data.put("srcTable", srcTable);
			data.put("targetTable",targetTable);
			data.put("dataHandler", dataHandler);
			data.put("uniqueKeys", uniqueKeys);
			
			data.put("extraFields", extraFields);
			
			
			List<Element> list = XMLUtil.getChildNodes(element);
			String[] srcFields = new String[list.size()];
			String[] targetFields = new String[list.size()];
			String[] converts = new String[list.size()];
			int index = 0;
			StringBuffer sBuffer = new StringBuffer("select ");
			StringBuffer rBuffer = new StringBuffer();
			StringBuffer deStringBuffer = new StringBuffer();
			boolean first = true;
			for (Element child : list) {
				srcFields[index] = child.getAttribute("src");
				if(first){
					first = false;
				}else{
					sBuffer.append(",");
					deStringBuffer.append(",");
					rBuffer.append(",");
				}
				sBuffer.append(srcFields[index]);
				
				rBuffer.append("r."+srcFields[index]);
				
				targetFields[index] = child.getAttribute("target");
				deStringBuffer.append("t."+targetFields[index]);
				
				
				
				converts[index] = child.getAttribute("convert");
				++index;
			}
			
			sBuffer.append(" from ").append(srcTable);
			
			data.put("srcFields", srcFields);
			data.put("targetFields", targetFields);
			data.put("converts", converts);
			
			if(method.equals(DbSyncUtil.METHOD_PAGE)){
				int pageSize = Integer.parseInt( element.getAttribute("pageSize") );
				data.put("pageSize", pageSize);
			}
			
			try {
				QuartzPlugin.me().startJob(DbSyncJob.class,
						element.getAttribute("corn"), element.getAttribute("name"),data);
			} catch (SchedulerException e1) {
				throw new RuntimeException(e1);
			}
			
		}
	};
}
